package com.cty.util.controller;

import com.cty.util.util.MDCUtils;
import com.cty.util.util.ThreadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * MDCController
 *
 * @author : cty
 * @since 2023/11/19
 */

@Slf4j
@RestController
@RequestMapping("/mdc")
public class MDCController {

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @GetMapping("/test")
    public String test() {
        try {
            MDCUtils.newLogId();
            log.info("/mdc/test 开始...");
            String traceId = MDCUtils.getLogId();
            log.info("/mdc/test 结束...traceId:{}", traceId);
            return traceId;
        } finally {
            MDCUtils.clear();
        }
    }

    @GetMapping("/testConcurrent")
    public String testConcurrent() {
        try {
            MDCUtils.newLogId();
            log.info("/mdc/testConcurrent 开始...");
            String traceId = MDCUtils.getLogId();
            log.info("/mdc/testConcurrent 结束...traceId:{}", traceId);

            ThreadUtils<Object> threadUtils = new ThreadUtils<>();
            Integer add = (Integer) threadUtils.asyncSubmit(() -> this.add(2, 6));
            String addStr = (String) threadUtils.asyncSubmit(() -> this.addStr("aa", "bb"));
            threadUtils.allOfJoin();
            threadUtils.asyncExecute(this::task);
            return add + " | " + addStr + " | " + traceId;


//            // 同一任务循环执行多次
//            ThreadUtils<String> threadUtils = new ThreadUtils<>();
//            for (int i = 0; i < 10; i++) {
//                final String iStr = String.valueOf(i);
//                threadUtils.asyncSubmit(() -> this.addStr(iStr, "-hhh"));
//            }
//            List<String> stringList = threadUtils.getAllOfJoinResult();
//            return stringList.toString() + " | " + traceId;

        } finally {
            MDCUtils.clear();
        }
    }

    /**
     * 数字加法
     */
    private Integer add(Integer a, Integer b) {
        log.info("/mdc/testConcurrent add() 开始...");
        Integer rs = a + b;
        log.info("/mdc/testConcurrent add() 结束...rs:{}, traceId:{}", rs, MDCUtils.getLogId());
        return rs;
    }

    /**
     * 字符串加法
     */
    private String addStr(String a, String b) {
        log.info("/mdc/testConcurrent addStr() 开始...");
        String rs = a + b;
        log.info("/mdc/testConcurrent addStr() 结束...rs:{}, traceId:{}", rs, MDCUtils.getLogId());
        return rs;
    }

    /**
     * 无返回任务
     */
    private Void task() {
        log.info("/mdc/testConcurrent task() 开始...");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("执行了一个没有返回的任务！");
        log.info("/mdc/testConcurrent task() 结束...traceId:{}", MDCUtils.getLogId());
        return null;
    }

}
